package org.mspring.mlog.web.module.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.web.support.log.Log;
import org.mspring.platform.dao.Order.OrderType;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.security.SecurityUtils;
import org.mspring.platform.security.entity.Resource;
import org.mspring.platform.security.service.ResourceService;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Gao Youbo
 * @since 2013-6-9
 */
@Widget
@RequestMapping("/admin/resource")
public class Admin_ResourceWidget extends Admin_AbstractWidget {

    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/list")
    @Log
    public String list(@ModelAttribute Page<Resource> resourcePage, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (resourcePage == null) {
            resourcePage = new Page<>();
        }
        resourcePage.addOrder("id", OrderType.DESC);
        resourcePage = resourceService.listPage(QueryBuilder.instance(Resource.class, queryParams), resourcePage);
        model.addAttribute("resourcePage", resourcePage);
        return "/admin/resource/listResource";
    }

    @RequestMapping("/create")
    public String create(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/resource/createResource";
    }

    @RequestMapping("/create/save")
    @Log
    public String create_save(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            resourceService.insert(resource);
            // 重新加载资源-权限对应关系
            SecurityUtils.reloadResourceDefine();
            return prompt(model, "", "资源增加成功", "/admin/resource/list");
        } catch (Exception e) {
            return prompt(model, "资源增加失败" + e.getMessage());
        }
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, @ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            Object obj = getSessionAttribute(request, "Admin_ResourceWidget_edit_id");
            if (obj != null) {
                id = (Long) obj;
            }
        }
        if (id == null) {
            return prompt(model, "请选择要修改的资源");
        }

        resource = resourceService.get(id);
        if (resource == null) {
            return prompt(model, "请选择要修改的资源");
        }
        model.addAttribute("resource", resource);
        setSessionAttribute(request, "Admin_ResourceWidget_edit_id", id);
        return "/admin/resource/editResource";
    }

    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute Resource resource, HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            resourceService.update(resource);
            // 重新加载资源-权限对应关系
            SecurityUtils.reloadResourceDefine();
            return prompt(model, "资源修改成功", "/admin/resource/edit?id=" + resource.getId());
        } catch (Exception e) {
            return prompt(model, "资源修改失败");
        }
    }

    @RequestMapping("/delete")
    @Log
    public String delete(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        // boolean flag = resourceService.deleteResource(id);
        // SecurityUtils.reloadResourceDefine();
        // if (flag) {
        // SecurityUtils.reloadResourceDefine();
        // return prompt(model, "资源删除成功", "/admin/resource/list");
        // }
        return prompt(model, "资源删除失败", "/admin/resource/list");
    }

}
